using System;
using System.Collections.Generic;

namespace Wikey.Domain.Components
{
    public class PagedList<T> {
        public List<T> Data { get; set; }
        public PagedInfo PagedInfo { get; private set; }
        public bool HasRecords {
            get { return PagedInfo.TotalRecords > 0; }
        }

        public PagedList() { }
        public PagedList(Pager pager, List<T> data, int totalRecords) {
            PagedInfo = new PagedInfo(pager, totalRecords);
            Data = data;
        }
    }

    public class PagedInfo {
        public int TotalRecords { get; set; }
        public int CurrentPage { get; set; }
        public int RecordsPerPage { get; set; }
        public int TotalPages {
            get {
                if (TotalRecords == 0 || RecordsPerPage == 0) {
                    return 0;
                }
                return (int)Math.Ceiling((float)TotalRecords / RecordsPerPage);
            }
        }

        public PagedInfo(Pager pager, int totalRecords) {
            TotalRecords = totalRecords;
            CurrentPage = pager.CurrentPage;
            RecordsPerPage = pager.RecordsPerPage;
        }
    }

    public class Pager {
        public int CurrentPage { get; set; }
        public int RecordsPerPage { get; set; }
        public int FirstRecord {
            get {
                return (CurrentPage - 1) * RecordsPerPage;
            }
        }

        public void MaximumRecordsPerPage(int maximum) {
            if (RecordsPerPage == 0 || RecordsPerPage > maximum) {
                RecordsPerPage = maximum;
            }
        }

        public Pager() { }
        public Pager(int currentPage) {
            CurrentPage = currentPage;
        }
        public Pager(int currentPage, int recordsPerPage) {
            CurrentPage = currentPage;
            RecordsPerPage = recordsPerPage;
        }
    }
}